package info.gridworld.actor;

import info.gridworld.grid.Location;

import java.awt.Color;
import java.util.ArrayList;

public class BlusterCritter extends Critter {
	
	private int c;
	private static final double DARKENING_FACTOR = 0.05;
	private static final double BRIGHTING_FACTOR = 0.05;
	
	public BlusterCritter(int c) {
		this.c = c;
	}
	
	public void darken() {
		Color color = getColor();
        int red = (int) (color.getRed() * (1 - DARKENING_FACTOR));
        int green = (int) (color.getGreen() * (1 - DARKENING_FACTOR));
        int blue = (int) (color.getBlue() * (1 - DARKENING_FACTOR));

        setColor(new Color(red, green, blue));
	}
	
	public void bright() {
		Color color = getColor();
        int red = color.getRed() + 1;
        int green = color.getGreen() + 1;
        int blue = color.getBlue() + 1;

        if(red > 255) {
        	red = 255;
        }
        if(blue > 255) {
        	blue = 255;
        }
        if(green > 255) {
        	green = 255;
        }
        
//        System.out.println("(" + red + "," + blue + "," + green + ")");
        setColor(new Color(red, green, blue));
	}
	
	protected ArrayList<Location> getLocationsWithinTwo() {
		ArrayList<Location> locations = new ArrayList<Location>();
		
		int currentRow = getLocation().getRow();
		int currentCol = getLocation().getCol();
		
		for (int i = currentRow - 2; i < currentRow + 2; i++) {
			for (int j = currentCol - 2; j < currentCol + 2; j++) {
				Location loc = new Location(i,j);
				if(getGrid().isValid(loc) && !loc.equals(getLocation())) {
					locations.add(loc);
				}
			}
		}
		return locations;
	}

	@Override
	public ArrayList<Actor> getActors() {
		
		ArrayList<Actor> actors = new ArrayList<Actor>();
		ArrayList<Location> locations = this.getLocationsWithinTwo();
		for(Location loc : locations) {
			Actor actor = getGrid().get(loc);
			if(actor != null && actor instanceof Critter) {
				actors.add(actor);
			}
		}
		return actors;
	}

	@Override
	public void processActors(ArrayList<Actor> actors) {
		
		if(actors.size() < c) {
			bright();
		}
		else {
			darken();
		}
	}
	
	
	
	

}
